package com.example.es.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import javaBean.Reg;
import com.example.es.myapplication.R;
import model.UserModel;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class RegisterActivity extends AppCompatActivity {

   // private EditText user_name;
    private EditText password;
   // private EditText uuserpass;
    private EditText tel;
    private Button btn_reg;

    String error;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        intViews();
        intEvents();
    }
    public void intViews(){

       // user_name = (EditText) findViewById(R.id.user_name);
        password = (EditText) findViewById(R.id.password);
        //uuserpass = (EditText) findViewById(R.id.ppwd);
        btn_reg = (Button) findViewById(R.id.btn_reg);
        tel =(EditText)findViewById(R.id.tel);
//        btn_reg.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent();
//                intent.setClass(RegisterActivity.this,LoginActivity.class);
//                startActivity(intent);
//            }
//        });
    }
    public void intEvents(){
        btn_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.btn_reg:
                        if ("".equals(tel.getText().toString()) || "".equals(password.getText().toString()))
                            Toast.makeText(RegisterActivity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
                        else
                            register();

                }
            }
                });

   }
    public  void register(){

                UserModel userModel = new UserModel();
                Call call = userModel.register(
                        password.getText().toString(),tel.getText().toString());
                Callback<Reg> callback = new Callback<Reg>() {
                    @Override
                    public void onResponse(Call<Reg> call, Response<Reg> response) {

                        Reg user = response.body();
                        if ("手机号已被使用".equals(user.getError())) {
                            Toast.makeText(RegisterActivity.this, "用户名已存在", Toast.LENGTH_SHORT).show();

                        } else {

                            Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_LONG).show();
                            Intent intent = new Intent();
//                            String user_name = tel.getText().toString();
//                            intent.putExtra("tel",user_name);
//                            setResult(RESULT_OK,intent);
                            intent.setClass(RegisterActivity.this, LoginActivity.class);
                            startActivity(intent);
                        }
                    }


                    @Override
                    public void onFailure(Call<Reg> call, Throwable t) {
                        Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_LONG).show();
                    }
                };

                call.enqueue(callback);
    }
}
